// app/model/supplier_evaluation_model.js
'use strict';

module.exports = app => {
  const { STRING, INTEGER, DATE, DECIMAL, TEXT, ENUM } = app.Sequelize;

  const SupplierEvaluationModel = app.model.define('supplier_evaluation', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    supplierId: {
      type: INTEGER,
      allowNull: false,
      field: 'supplier_id',  // 改为下划线命名
      comment: '供应商ID'
    },
    evaluator: {
      type: STRING(50),
      allowNull: false,
      field: 'evaluator',
      comment: '评估人'
    },
    evaluationDate: {
      type: DATE,
      allowNull: false,
      defaultValue: new Date(),
      field: 'evaluation_date',  // 改为下划线命名
      comment: '评估日期'
    },
    qualityScore: {
      type: DECIMAL(3, 2),
      allowNull: false,
      field: 'quality_score',  // 改为下划线命名
      comment: '质量评分(1-5分)'
    },
    deliveryScore: {
      type: DECIMAL(3, 2),
      allowNull: false,
      field: 'delivery_score',  // 改为下划线命名
      comment: '交货评分(1-5分)'
    },
    serviceScore: {
      type: DECIMAL(3, 2),
      allowNull: false,
      field: 'service_score',  // 改为下划线命名
      comment: '服务评分(1-5分)'
    },
    priceScore: {
      type: DECIMAL(3, 2),
      allowNull: false,
      field: 'price_score',  // 改为下划线命名
      comment: '价格评分(1-5分)'
    },
    overallScore: {
      type: DECIMAL(3, 2),
      allowNull: false,
      field: 'overall_score',  // 改为下划线命名
      comment: '综合评分(1-5分)'
    },
    evaluationPeriod: {
      type: STRING(50),
      allowNull: true,
      field: 'evaluation_period',  // 改为下划线命名
      comment: '评估周期(如: 2025年Q1)'
    },
    status: {
      type: ENUM('draft', 'submitted', 'approved'),
      defaultValue: 'draft',
      field: 'status',
      comment: '评估状态: draft(草稿), submitted(已提交), approved(已审核)'
    },
    remark: {
      type: TEXT,
      allowNull: true,
      field: 'remark',
      comment: '评估备注'
    },
    createdAt: {
      type: DATE,
      allowNull: false,
      defaultValue: new Date(),
      field: 'created_at',  // 改为下划线命名
      comment: '创建时间'
    },
    updatedAt: {
      type: DATE,
      allowNull: false,
      defaultValue: new Date(),
      field: 'updated_at',  // 改为下划线命名
      comment: '更新时间'
    }
  }, {
    tableName: 'supplier_evaluations',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at'
  });

  // 关联关系
  SupplierEvaluationModel.associate = function() {
    app.model.SupplierEvaluationModel.belongsTo(app.model.SupplierModel, {
      as: 'supplier',
      foreignKey: 'supplier_id'
    });
  };

  return SupplierEvaluationModel;
};